package cn.aiszs.javafx_imp.service;

import cn.aiszs.javafx_imp.mapper.OilMapper;
import cn.aiszs.javafx_imp.pojo.Oil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OilService {

    @Autowired
    private OilMapper oilMapper;

    @Autowired
    private Oil oil;

    public Oil insertData(String data){
        JSONObject jsonObject = JSON.parseObject(data);//Json字符串解析成对象
        oil = new Oil();
        oil.setProvince(jsonObject.getString("province"));
        oil.setP0(jsonObject.getJSONObject("data").getDouble("p0"));
        oil.setP89(jsonObject.getJSONObject("data").getDouble("p89"));
        oil.setP92(jsonObject.getJSONObject("data").getDouble("p92"));
        oil.setP95(jsonObject.getJSONObject("data").getDouble("p95"));
        oil.setUpdatetime(jsonObject.getDate("update_time"));
        oilMapper.insert(oil);//默认雪花算法
        return oil;
    }

    public Boolean is_exist(String province){
        QueryWrapper<Oil> wrapper = new QueryWrapper<>();
        wrapper.eq("province",province);
        return oilMapper.selectOne(wrapper) != null;
    }

    public Oil selectByProvince(String province){
        return oilMapper.findProvince(province);
    }
}
